from pymediainfo import MediaInfo
import json
def m_info(vp):
    media_info = json.loads(MediaInfo.parse(vp).to_json())
    film_info = []
    for track in media_info['tracks']:
        track_type = track.get('track_type', '')
        if track_type in ['Video', 'Audio']:
            try:
                bit_rate = int(track['bit_rate'])
            except (KeyError, ValueError):
                bit_rate = None  
            if track_type == 'Video':
                try:
                    sampled_width = int(track['sampled_width'])
                    sampled_height = int(track['sampled_height'])
                except (KeyError, ValueError):
                    sampled_width = sampled_height = None                 
                info = {
                    '视频格式': track.get('format', ''),
                    '视频码率': '%dkb' % (bit_rate / 1000) if bit_rate is not None else '',
                    '宽高比': '%dX%d' % (sampled_width, sampled_height) if sampled_width and sampled_height else '',
                    '帧率': int(float(track['frame_rate'])) if 'frame_rate' in track else '',
                    '视频流大小': '%.2f MB' % (track['stream_size'] / 1024**2) if 'stream_size' in track else '',
                    '视频时长': track['other_duration'][-2] if 'other_duration' in track and track['other_duration'] else ''
                }
                
            elif track_type == 'Audio':
                info = {
                    '音频格式': track.get('format', ''),
                    '音频码率': '%dkb' % (bit_rate / 1000) if bit_rate is not None else '',
                    '音频时长': track['other_duration'][0] if 'other_duration' in track and track['other_duration'] else '',
                    '音频流大小': '%.2f MB' % (track['stream_size'] / 1024**2) if 'stream_size' in track else ''
                }
            try:
                film_info.append(info)
            except Exception as e:
                # 更细致的异常处理可以在这里增加
                print(f"Error processing track info: {e}")
    return film_info
